/**
 * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.opsli.modulars.tenant.customerstatus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.opsli.api.wrapper.tenant.customerstatus.TenantCustomerStatusModel;
import org.opsli.core.base.service.impl.CrudServiceImpl;
import org.opsli.modulars.tenant.customerstatus.entity.TenantCustomerStatus;
import org.opsli.modulars.tenant.customerstatus.mapper.TenantCustomerStatusMapper;
import org.opsli.modulars.tenant.customerstatus.service.ITenantCustomerStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 租户客户状态配置 Service Impl
 *
 * @author guagua
 * @date 2025-01-04
 */
@Service
public class TenantCustomerStatusServiceImpl
        extends CrudServiceImpl<TenantCustomerStatusMapper, TenantCustomerStatus, TenantCustomerStatusModel>
        implements ITenantCustomerStatusService {

    @Autowired(required = false)
    private TenantCustomerStatusMapper mapper;

    @Override
    public List<TenantCustomerStatusModel> findAllActive() {
        QueryWrapper<TenantCustomerStatus> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_active", 1)
                .eq("deleted", 0)
                .orderByAsc("sort_order");

        List<TenantCustomerStatus> entityList = mapper.selectList(queryWrapper);
        return super.transformTs2Ms(entityList);
    }

    @Override
    public TenantCustomerStatusModel getByStatusCode(String statusCode) {
        QueryWrapper<TenantCustomerStatus> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status_code", statusCode)
                .eq("is_active", 1)
                .eq("deleted", 0)
                .last("LIMIT 1");

        TenantCustomerStatus entity = mapper.selectOne(queryWrapper);
        return super.transformT2M(entity);
    }

    @Override
    public void initCoreStatus() {
        // 检查是否已存在核心状态
        QueryWrapper<TenantCustomerStatus> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status_type", "CORE");

        long count = mapper.selectCount(queryWrapper);
        if (count > 0) {
            return; // 已存在核心状态，不重复初始化
        }

        // 创建核心状态：正常
        TenantCustomerStatus normalStatus = new TenantCustomerStatus();
        normalStatus.setStatusCode("1");
        normalStatus.setStatusName("正常");
        normalStatus.setStatusType("CORE");
        normalStatus.setParentType("NORMAL");
        normalStatus.setStatusColor("success");
        normalStatus.setSortOrder(1);
        normalStatus.setIsSystem(1);
        normalStatus.setIsActive(1);
        normalStatus.setRemark("系统核心状态 - 正常客户");
        mapper.insert(normalStatus);

        // 创建核心状态：流失
        TenantCustomerStatus lostStatus = new TenantCustomerStatus();
        lostStatus.setStatusCode("0");
        lostStatus.setStatusName("流失");
        lostStatus.setStatusType("CORE");
        lostStatus.setParentType("LOST");
        lostStatus.setStatusColor("error");
        lostStatus.setSortOrder(2);
        lostStatus.setIsSystem(1);
        lostStatus.setIsActive(1);
        lostStatus.setRemark("系统核心状态 - 流失客户");
        mapper.insert(lostStatus);
    }
}